package com.lcc.lynx.base.system.mapper;

import com.lcc.lynx.base.system.pojo.def.SYS_MENU;
import com.lcc.lynx.base.system.pojo.def.SYS_ROLE;
import com.lcc.lynx.base.system.pojo.def.SYS_ROLE_MENU;
import com.lcc.lynx.base.system.pojo.def.SYS_USER_ROLE;
import com.lcc.lynx.common.utils.AssertUtil;
import com.lcc.lynx.jdbc.IBaseMapper;
import com.lcc.lynx.base.system.pojo.dv.SysUserDV;

import static com.lcc.lynx.jdbc.qw.QueryMethods.*;

import com.lcc.lynx.jdbc.qw.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 系统用户Mapper
 *
 * @auth lcc
 * @date 2024-12-31
 */
@Mapper
public interface SysUserMapper extends IBaseMapper<SysUserDV> {


    /**
     * 查询用户角色
     *
     * @param userInfo
     * @return
     * @author lcc
     * @date 2025/1/16 16:57
     */
    default List<String> queryUserRoles(SysUserDV userInfo) {
        AssertUtil.isNotNull(userInfo.getUserId(), "用户ID不能为空");
        return QueryWrapper.resultType(String.class)
                .select(SYS_ROLE.role_code)
                .from(SYS_USER_ROLE.class, SYS_ROLE.class)
                .where(
                        SYS_USER_ROLE.user_id.eq(userInfo.getUserId()),
                        SYS_USER_ROLE.role_id.eq(SYS_ROLE.role_id)
                ).list();
    }

    /**
     * 查询用户权限
     *
     * @param userInfo
     * @return
     * @author lcc
     * @date 2025/1/20 10:50
     */

    default List<String> queryUserPermissions(SysUserDV userInfo) {
        AssertUtil.isNotNull(userInfo.getUserId(), "用户ID不能为空");
        return QueryWrapper.resultType(String.class)
                .select(distinct(), SYS_MENU.permission)
                .from(SYS_USER_ROLE.class, SYS_ROLE_MENU.class, SYS_MENU.class)
                .where(
                        SYS_USER_ROLE.user_id.eq(userInfo.getUserId()),
                        SYS_USER_ROLE.role_id.eq(SYS_ROLE_MENU.role_id),
                        SYS_ROLE_MENU.menu_id.eq(SYS_MENU.menu_id),
                        SYS_MENU.state.eq((short) 1),
                        SYS_MENU.permission.isNotNull(),
                        SYS_MENU.menu_type.eq((short) 3)
                ).list();
    }
}